package com.qf.homework.controller;

import com.qf.homework.entity.User;
import com.qf.homework.service.UserService;
import com.qf.homework.service.impl.UserServiceImpl;
import com.qf.homework.utils.ResponseEntity;
import com.qf.homework.utils.ResponseUtils;
import com.qf.homework.utils.StatUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/User/login")
public class LoginController extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //非空处理
        ResponseEntity responseEntity=null;
        if (username==null||password==null) { ResponseUtils.fail(resp, StatUtil.NULL_ACCOUNT); return;}
        try {
            User user = userService.login(username, password);
            responseEntity=new ResponseEntity();
            if (user==null){ResponseUtils.fail(resp,StatUtil.USER_ACCOUNT_ERR);}
            else {ResponseUtils.success(resp,responseEntity,user,StatUtil.SUCCESS);}
        } catch (SQLException throwable) {
            ResponseUtils.fail(resp,StatUtil.SERVER_BUST);
        }
    }
}
